#include "MainWindow.h"
#include <iostream>
#include "digital_process.h"
static const char* yuv_strings[COMPONENT_COUNT] = {
	"Y", "U", "V"
};

int MainWindow::update_output_color()
{
	int Y = com_choose[COMPONENT_Y].slider->value();// line_edit->text().toInt();
	int U = com_choose[COMPONENT_U].slider->value();
	int V = com_choose[COMPONENT_V].slider->value();

	unsigned char R,G,B;
	get_rgb_by_yuv_pixel(Y, U, V, &R, &G, &B);
	QString strSerInfo = QString("background-color: rgb(%1, %2, %3);").arg(R).arg(G).arg(B);
	color_display_area->setStyleSheet(strSerInfo);
	return 0;	
}
void MainWindow::y_slider_val_change(int val)
{
	//std::cout<<"y_slider_val_change" <<std::endl;
	com_choose[COMPONENT_Y].line_edit->setText(QString::number(val));
	update_output_color();
	return ;
}

void MainWindow::u_slider_val_change(int val)
{
	com_choose[COMPONENT_U].line_edit->setText(QString::number(val));
	update_output_color();
	return ;
}

void MainWindow::v_slider_val_change(int val)
{
	com_choose[COMPONENT_V].line_edit->setText(QString::number(val));
	update_output_color();
	return ;
}


MainWindow::MainWindow()
{
    this->resize(700, 500);

	central_widget = new QWidget(this);
	central_widget->resize(this->width(), this->height());
	central_widget->show();
	central_widget->setStyleSheet("border:1px solid read");
	central_widget_layout = new QGridLayout(central_widget);
	
	color_display_area = new QWidget();
	central_widget_layout->addWidget(color_display_area, 0, 0, 1, 1);
	central_widget_layout->setColumnStretch(0, 30);
	//color_display_area->setStyleSheet("background-color:green;");
	color_display_area->setStyleSheet("border:1px solid black");
	//color_display_area->setGeometry(0, 0, 500, 500);
	color_display_area->show();
	
	color_select_area = new QWidget();
	central_widget_layout->addWidget(color_select_area, 0, 1, 1, 1);
	central_widget_layout->setColumnStretch(1, 10);
	//color_select_area->setStyleSheet("background-color:blue;");
	//color_select_area->resize(700, 500);
	color_select_area->show();
	color_select_area->setStyleSheet("border:1px solid black");
	
	color_select_area_layout = new QGridLayout(color_select_area);
	

	int i = 0;
	for(i = 0; i < COMPONENT_COUNT; ++i) {
		com_choose[i].label = new QLabel();
		color_select_area_layout->addWidget(com_choose[i].label, 0, i, 1, 1, Qt::AlignHCenter);
		com_choose[i].label->setText(yuv_strings[i]);
		com_choose[i].label->setStyleSheet("border:1px solid black");
		
		com_choose[i].line_edit = new QLineEdit();
		color_select_area_layout->addWidget(com_choose[i].line_edit, 1, i, 1, 1, Qt::AlignHCenter);	
		com_choose[i].line_edit->setStyleSheet("border:1px solid black");
		
		com_choose[i].slider = new QSlider();
		color_select_area_layout->addWidget(com_choose[i].slider, 2, i, 1, 1, Qt::AlignHCenter);
		com_choose[i].slider->setRange(16, 240);
		com_choose[i].slider->setStyleSheet("border:1px solid black");
	}
	com_choose[COMPONENT_Y].slider->setRange(16, 235);
	connect(com_choose[COMPONENT_Y].slider, SIGNAL(valueChanged(int)), this, SLOT(y_slider_val_change(int)));
	connect(com_choose[COMPONENT_U].slider, SIGNAL(valueChanged(int)), this, SLOT(u_slider_val_change(int)));
	connect(com_choose[COMPONENT_V].slider, SIGNAL(valueChanged(int)), this, SLOT(v_slider_val_change(int)));
	#if 0
	color_select_area_layout.setHorizontalSpacing(10);
	color_select_area_layout.setVerticalSpacing(10);
	color_select_area_layout.setContentsMargins(10, 10, 10, 10);
	#endif

//	color_select_area_layout.setParent(this);
	/*
	but.setParent(this);
	but.setText("ssssssssss");
	but.setStyleSheet("background-color:blue;");
	but.resize(50,20);
	but.show();
	*/
}

void MainWindow::paintEvent(QPaintEvent *event)
{
	central_widget->resize(this->width(), this->height());
	//int width = this->width();
	//int height = this->height();
	//color_display_area
}
